<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
    
	<script type="text/javascript">
		//NetServer参数列表数据源
		var data_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["id","ip","port","name","state"],
    		url: "netServer!queryList.action"
    	});
    	//页面初始化
		
			//定义数据显示面板
			var p1 = {
				id: "p1",
				xtype: "grid",
				stripeRows:true,
				loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
				autoExpandColumn: "name",
				autoExpandMin : 100,//自动填充最小宽度
				store: data_store,
				headersDisabled: true,
				columns:[
	    	    	new Ext.grid.RowNumberer({header: "序号", width: 35}),
    					{header: "ID号",		dataIndex: "id",menuDisabled: true},
    					{header: "IP地址",	dataIndex: "ip",menuDisabled: true},
    					{header: "IP端口号",	dataIndex: "port",menuDisabled: true},
    					{header: "服务名称",	dataIndex: "name",menuDisabled: true,id: "name"},
    					{
    						xtype:"billActionColumn",
    						header: "启用",
    						dataIndex: "state",
    						width:34,
    						menuDisabled: true,
    						items:[{
			                    icon:"images/shared/icons/fam/accept.jpg",  // Use a URL in the icon config
			                    tooltip:"启用",
			                    handler: function(grid, rowIndex, colIndex) {
			                    	Ext.Msg.confirm('警告','是否确定开启服务？',function(button) {
			                    		if(button=='yes') {
			                    			Ext.Ajax.request({
												clientValidation: true,
												params: {
													"netServer.ip":data_store.getAt(rowIndex).get('ip'),
													"netServer.port":data_store.getAt(rowIndex).get('port'),
													"netServer.state":1
												},
												url: "netServer!operServer.action",
												success: function(response, opts){
													var d=eval("("+response.responseText+")");
													if(d.data.indexOf("成功") >= 0){
														var record = data_store.getAt(rowIndex);
						                    			record.data.state='1';
						                    			data_store.remove(record);
						                    			data_store.insert(rowIndex,record);
													}
													Ext.Msg.show({title:"提示",
								                       msg:d.data,
								                       buttons:Ext.Msg.OK,
								                       icon: Ext.MessageBox.INFO
							                     	});
												}
											});
			                    		}
			                    	});
			                    }
			                }],
			                renderer: function(input){
			                	return "2"==input;
    						}
    					},{
    						xtype:"billActionColumn",
    						header: "停止",
    						dataIndex: "state",
    						width:34,
    						menuDisabled: true,
    						items:[{
			                    icon:"images/shared/icons/fam/delete.gif",  // Use a URL in the icon config
			                    tooltip:"停止",
			                    handler: function(grid, rowIndex, colIndex) {
									Ext.Msg.confirm('警告','是否确定停止服务？',function(button) {
			                    		if(button=='yes') {
			                    			Ext.Ajax.request({
												clientValidation: true,
												params: {
													"netServer.ip":data_store.getAt(rowIndex).get('ip'),
													"netServer.port":data_store.getAt(rowIndex).get('port'),
													"netServer.state":2
												},
												url: "netServer!operServer.action",
												success: function(response, opts){
													var d=eval("("+response.responseText+")");
													if(d.data.indexOf("成功") >= 0){
														var record = data_store.getAt(rowIndex);
						                    			record.data.state='2';
						                    			data_store.remove(record);
						                    			data_store.insert(rowIndex,record);
													}
													Ext.Msg.show({title:"提示",
								                       msg:d.data,
								                       buttons:Ext.Msg.OK,
								                       icon: Ext.MessageBox.INFO
							                     	});
												}
											});
			                    		}
			                    	});
			                    }
			                }],
			                renderer: function(input){
			                	return "1"==input;
    						}
    					},{
    						xtype:"billActionColumn",
    						header: "重启",
    						dataIndex: "state",
    						width:34,
    						menuDisabled: true,
    						items:[{
			                    icon:"js/ext/resources/images/default/grid/refresh.gif",  // Use a URL in the icon config
			                    tooltip:"重启",
			                    handler: function(grid, rowIndex, colIndex) {
									Ext.Msg.confirm('警告','是否确定重启服务？',function(button) {
			                    		if(button=='yes') {
			                    			Ext.Ajax.request({
												clientValidation: true,
												params: {
													"netServer.ip":data_store.getAt(rowIndex).get('ip'),
													"netServer.port":data_store.getAt(rowIndex).get('port'),
													"netServer.state":3
												},
												url: "netServer!operServer.action",
												success: function(response, opts){
													var d=eval("("+response.responseText+")");
													if(d.data.indexOf("成功") >= 0){
														var record = data_store.getAt(rowIndex);
						                    			record.data.state='1';
						                    			data_store.remove(record);
						                    			data_store.insert(rowIndex,record);
													}
													Ext.Msg.show({title:"提示",
								                       msg:d.data,
								                       buttons:Ext.Msg.OK,
								                       icon: Ext.MessageBox.INFO
							                     	});
												}
											});
			                    		}
			                    	});
			                    }
			                }],
			                renderer: function(input){
			                	return "1"==input;
    						}
    					}
				],
				buttonAlign: "center",
				bbar: new Ext.ux.PagingToolbar({
    	        	pageSize:limit,
    	        	store: data_store
    	    	}),
				buttons:[{
					text:"新增", 
					handler: function(){
						oper_type = "add";
						Ext.getCmp("p2").resetParam();
						Ext.getCmp("mycontainer").layout.setActiveItem(1);
					}
				},{
					text:"修改", 
					handler: function(){
						var p1 = this.ownerCt.ownerCt;
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if (record){
							Ext.getCmp("p2").setParam(record.data);
							oper_type = "upd";
							Ext.getCmp("mycontainer").layout.setActiveItem(1);
						} else {
							Ext.Msg.show({
	                          title:"警告",
	                          msg: "请先选择一条记录，然后点击修改!",
	                          buttons: Ext.Msg.OK,
	                          animEl: "elId",
	                          icon:Ext.MessageBox.WARNING
                          	});
						}	
					}
				},{
					text:"删除", 
					handler: function(){
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if(record){
							Ext.Msg.confirm("警告","确定要删除吗?",function(button) {
								if(button == "yes") {
									var url = "netServer!del.action";
									var params = {
										"netServer.id":record.data.id
									};
									oper(url,params);
								}
							});
						} else {
							Ext.Msg.show({
	                             title:"警告",
	                             msg: "没有任何行被选中，无法进行删除操作!",
	                             buttons: Ext.Msg.OK,
	                             animEl: "elId",
	                             icon:Ext.MessageBox.WARNING
                        	});
						}
				}}]
			};
			var p2 = {
				id: "p2",
			    labelWidth: 60,
				xtype: "form",
			    bodyStyle:"padding:5px 5px 0",
				defaultType: "textfield",
				items: [{
					id: "netServer.id", 
					fieldLabel: "编号",	
					name: "netServer.id",
					anchor: "100%"
				},{
					id: "netServer.ip", 
					fieldLabel: "IP地址",
					allowBlank: false,
					vtype:"ip",
					name: "netServer.ip",
					anchor: "100%"
				},{
					id: "netServer.port", 	  
					fieldLabel: "IP端口",	
					xtype:"numberfield",
					maxValue:65536,
					allowBlank: false,
					name: "netServer.port",
					anchor: "100%"
				},{
					id: "netServer.name", 	  
					fieldLabel: "服务名称",	
					allowBlank: false,
					vtype:"alphanumchinese",
					name: "netServer.name",
					anchor: "100%",
						validator:function (val){
			        		if(val=="")return "该输入项为必输项!";
			        		if(Ext.fucCheckCHLength(val)>50){
			        			return "请输入50个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
			        		}
			        		return true;
			        	}
				}],
				buttonAlign: "center",
				buttons:[{text:"保存", id: "save",handler: function(e){
					if(!Ext.getCmp("p2").getForm().isValid()){
						//Ext.Msg.alert("提示","验证不通过！");
						Ext.Msg.show({
                            title:"提示",
                            msg:"验证不通过!",
                            buttons:Ext.Msg.OK,
                            icon: Ext.MessageBox.ERROR
                        });
						return;
					}
					
					e.setDisabled(true);
					var form = this.ownerCt.ownerCt.getForm();
					var id = Ext.getCmp("netServer.id").getValue();
					var ip = Ext.getCmp("netServer.ip").getValue();
					var port = Ext.getCmp("netServer.port").getValue();
					var name = Ext.getCmp("netServer.name").getValue();
					var url,params;
					if(oper_type == "add") {
						url="netServer!add.action";
					} else if(oper_type == "upd") {
						url="netServer!upd.action";
					}
					params = {
						"netServer.id": id,
						"netServer.ip": ip,
						"netServer.port": port,
						"netServer.name": name
					};
					oper(url,params);
				}},{text:"取消", handler: function(){
					Ext.getCmp("mycontainer").layout.setActiveItem(0);
				}
			}],
			setParam: function(data){
				Ext.getCmp("netServer.id").setValue(data.id);
				Ext.getCmp("netServer.id").setDisabled(true);
				Ext.getCmp("netServer.id").setVisible(true);
				Ext.getCmp("netServer.ip").setValue(data.ip);
				Ext.getCmp("netServer.port").setValue(data.port);
				Ext.getCmp("netServer.name").setValue(data.name);
			},
			resetParam: function(){
				Ext.getCmp("netServer.id").setVisible(false);
				Ext.getCmp("netServer.ip").setValue("");
				Ext.getCmp("netServer.port").setValue("");
				Ext.getCmp("netServer.name").setValue("");
			}
		};
		
		var net_pel = new Ext.Panel({
			id: "mycontainer", 
			border : false,		
			layout: "card",
			activeItem: 0,
			items: [p1, p2]
		});
		
		data_store.load({params:{start:0,limit:limit}});
		
		var oper = function(url, params){
			Ext.Ajax.request({
				clientValidation: true,
				params: params,
				url: url,
				success: function(response, opts){
					var d=eval("("+response.responseText+")");
					if(d.code == "1"){
						Ext.getCmp("p1").getBottomToolbar().doRefresh();
						Ext.Msg.show({
	                       title:"提示",
	                       msg:"操作成功!",
	                       buttons:Ext.Msg.OK,
	                       icon: Ext.MessageBox.INFO
	                      });
						Ext.getCmp("mycontainer").layout.setActiveItem(0);
					} else {
						
							Ext.Msg.show({
		                      title:"提示",
		                      msg:"操作失败!",
		                      buttons:Ext.Msg.OK,
		                      icon: Ext.MessageBox.ERROR
		                       }); 
					}
					
		            Ext.getCmp("save").setDisabled(false);
				}
			});
		};
			
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(net_pel);
	CenterPanel.doLayout();
	</script>
